package com.ming.seatMonitoring.controller;

import com.ming.seatMonitoring.pojo.DO.SeatManageDO;
import com.ming.seatMonitoring.pojo.DO.UserInfoDO;
import com.ming.seatMonitoring.pojo.entity.ResultBody;
import com.ming.seatMonitoring.util.ExcelUtil;
import com.ming.seatMonitoring.util.JWTUtil;
import com.ming.seatMonitoring.util.LocalUser;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 小明
 * @date 2021/10/26
 * @description excel 文件读取
 */
@RestController
public class ExcelController {
    @RequestMapping("file/upload")
    public ResultBody pubggupload(@RequestParam("file") MultipartFile file) throws Exception {
        String name=file.getOriginalFilename();
        System.out.println(name);
        assert name != null;
        if(name.length()<6|| !".xlsx".equals(name.substring(name.length()-5))){
            return ResultBody.error().message("文件格式错误，请用.xlsx格式");
        }
        List<Object>list = ExcelUtil.excelToShopIdList(file.getInputStream());
        System.out.println(list);

        return ResultBody.ok().data("name",list);
    }

    /**
     * @param file 文件
     * @param x 数组x行
     * @param y 数组y列
     * @return
     * @throws Exception
     */
    @RequestMapping("file/uploadtest")
    public ResultBody pubggupload(@RequestParam("file")MultipartFile file,@RequestParam int x,@RequestParam int y) throws Exception {
        String name=file.getOriginalFilename();
        System.out.println(name);
        if(name.length()<6|| !".xlsx".equals(name.substring(name.length()-5))){
            List<Object>li=new ArrayList<>();
            li.add("文件格式错误");
            return ResultBody.error().message("文件格式错误，请用.xlsx格式");
        }
        LinkedList<Object> list = (LinkedList<Object>) ExcelUtil.excelToShopIdList(file.getInputStream());

        SeatManageDO seatManageDO = new SeatManageDO();


        String[][] strs = new String[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                Object poll = list.poll();
                strs[i][j] = (String) poll;

                seatManageDO.setRoomId(1001);
                seatManageDO.setBId(1);
                seatManageDO.setAxisX(String.valueOf(i));
                seatManageDO.setAxisY(String.valueOf(j));
                seatManageDO.setCreateTime(new Date());
                seatManageDO.setSeatName((String) poll);


            }
        }
        System.out.println(list);

        return ResultBody.ok().data("name",list);
    }
}
